﻿using System;
using System.Runtime.Caching;

namespace nanoCMS.Core.Providers
{
    public class CacheProvider : Interfaces.ICache
    {
        private static CacheProvider _instance;
        private MemoryCache memoryCache = MemoryCache.Default;
        public static CacheProvider Default
        {
            get { return _instance ?? (_instance = new CacheProvider()); }
        }

        public object Get(string key)
        { 
            return memoryCache.Get(key);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="cacheItemPolicy"></param>
        public void Add(string key, object value, CacheItemPolicy cacheItemPolicy = null)
        {
            //Default 5min
            if (cacheItemPolicy == null) new CacheItemPolicy() {SlidingExpiration = new TimeSpan(0, 0, 5, 0)};
            memoryCache.Add(key, value, cacheItemPolicy);
        }

        public bool Contains(string key)
        {
            return memoryCache.Contains(key);
        }

        public void Remove(string key)
        {
            memoryCache.Remove(key);
        }
    }
}
